var main = {
	init: function() {
		main.tabs = $('#tabs');
    	main.tabMenu = $('#tabMenu');
		main.nav = $('#mainNav');
		main.password = $('.updatePassword');
		main.oldPassword = $('#txtOldPass');
		main.userInfo = $('.userInfo');
		main.updateUserInfoDialog = $('#updateUserInfoDialog');
		main.updatePasswordDialog = $('#updatePasswordDialog');
		
		main.userInfo.click(function(){
			$.ajax({  
                type : 'POST',  
                url : window.ctx +'/sys/user/showUserInfoByLoginer',
                success : function(result) {
                	$('#userInfoCode').textbox('setValue',result.code);
                	$('#userInfoName').textbox('setValue',result.name);
                	$('#userRoleName').textbox('setValue',result.roleName);
                }  
            });  
			main.updateUserInfoDialog.dialog('open');
		});
		main.password.click(function(){
			$("#updatePasswordForm").form('reset');
			main.updatePasswordDialog.dialog('open');
			$('.passwordUpBtn').linkbutton("disable");
			$('.txtOldPass').blur(function(){
				console.info($('.txtOldPass').val());
				$.ajax({  
	                type : 'POST',  
	                url : window.ctx +'/sys/user/equalPassword',
	                data:{'password' : $('.txtOldPass').val()},
	                success : function(result) {
	                	if(result){
	                		$('.passwordUpBtn').linkbutton("enable");
	                	}else{
	                		layer.msg('原密码错误', {
	                		    time: 700
	                		  });
	                	}
	                }  
	            });
			});
    	});
    	//创建左侧导航菜单
    	main.nav.tree({
			url: window.ctx+ '/sys/menu/showLeftMenuTree.action',
			lines: true,
			onLoadSuccess: function(node, data) {
				main.nav.tree('expandAll')
			},
			onClick: function(node) {
				if (node.url) {
					if (main.tabs.tabs('exists', node.text)) {
						main.tabs.tabs('select', node.text)
					} else { 
						main.tabs.tabs('add', {
							title: node.text,
							iconCls: node.iconCls,
							closable: true,
							href: node.url
						});
			 		} 
				}
			}
		});
    	
        // 初始化tab，并创建右键菜单
    	main.tabs.tabs({
			border: false,
			fit: true,
            onContextMenu: function (e, title, index) {
                e.preventDefault();
                if (index > 0) {
                	main.tabMenu.menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    }).data("tabTitle", title);
                }
            }
        });
    	
        // 右键菜单click
        main.tabMenu.menu({
            onClick: function (item) {
	            var allTabs = main.tabs.tabs('tabs');
	            var allTabtitle = [];
	            $.each(allTabs, function (i, v) {
	                var opt = $(v).panel('options');
	                if (opt.closable) {
	                    allTabtitle.push(opt.title);
	                }
	            });
	            var curTabTitle = $(this).data("tabTitle");
	            var curTabIndex = main.tabs.tabs("getTabIndex", main.tabs.tabs("getTab", curTabTitle));
	            switch (item.name) {
                	case 'refresh':
                    	main.tabs.tabs("getTab", curTabTitle).panel("refresh");
                    	break;
                    case 'closeCurrent':
                    	console.info(curTabTitle+curTabIndex)
	                    main.tabs.tabs("close", curTabIndex);
	                    break;
	                case 'closeOther':
	                    $.each(allTabtitle, function(i, v) {
	                    	console.info(i)
	                        if (curTabTitle != allTabtitle[i]) {
	                            main.tabs.tabs('close', allTabtitle[i]);
	                        }
	                    });
	                    main.tabs.tabs('select', curTabTitle);
	                    break;
	                    
	                case 'closeRight':
	                	$.each(allTabtitle, function(i, v) {
	                		if ( i >= curTabIndex) {
	                            main.tabs.tabs('close', allTabtitle[i]);
	                        } 
	                    }); 
	                    main.tabs.tabs('select', curTabTitle); 
	                    break;
	                    
	              	case 'closeLeft':
	              		$.each(allTabtitle, function(i, v) {
	              			 if ( i < curTabIndex-1  ) {
	                            main.tabs.tabs('close', allTabtitle[i]);
	                        }  
	                    }); 
	                    main.tabs.tabs('select', curTabTitle); 
	                    break;
	                
	                case 'closeAll':
	                    $.each(allTabtitle, function(i, v) {
	                        main.tabs.tabs('close', allTabtitle[i]);
	                    });
	                    break;
	            }
            }
        });
	},
	btn:{
		updatePassword:function(){
			var ops = $('.txtOldPass').val();
			var nps = $('.txtNewPass').val();
			var rps = $('.txtRePass').val();
			if(((ops!=null && ops!='') && (rps!=null && rps!='') && (nps!=null&&nps!='')) && (nps == rps)){
				$.ajax({  
	                type : 'POST',  
	                url : window.ctx +'/sys/user/updatePassword',
	                data : {newPassword : nps},
	                success : function(result) {
	                	layer.confirm('修改密码成功，请退出登录？', {
                	       icon: 4,
                	       btn: ['确认','取消']
                		}, function(){
                		  location.href= window.ctx+'/sys/user/userLogout'; 
                		},function(){
                		  location.href= window.ctx+'/views/sys/index.jsp'; 
                	 	});
	                	main.updatePasswordDialog.dialog('close');
	                }  
	            });
			}else{
				$.messager.alert('错误','信息未填写完善！','error');
			}
		},
		redoPassword: function(){
			main.updatePasswordDialog.dialog('close');
		},
		redoUserInfo: function() {
			main.updateUserInfoDialog.dialog('close');
		}
	}
};

$(function(){
	main.init();
});
